﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.TcCommands.TcReplaceJt.CApTcReplaceJtModel
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using Tecnomatix.Engineering;
using Tecnomatix.Planning;


namespace DnProcessSimulateCommands.TcCommands.TcReplaceJt;

public class CApTcReplaceJtModel
{
  private ITxPlanningInstance m_selectedInstance;
  private TxCamera m_savedCamera;
  private List<TxProcessModelId> m_savedSelection;

  public CApTcReplaceJtModel(ITxPlanningInstance selectedInstance)
  {
    this.m_selectedInstance = selectedInstance != null ? selectedInstance : throw new ArgumentNullException(nameof (selectedInstance));
    this.Initialize();
  }

  public List<string> Instances { get; set; }

  public string CurrentJtName { get; set; }

  public string NewJtPath { get; set; }

  public bool DoesHaveJT => !string.IsNullOrWhiteSpace(this.CurrentJtName);

  public TxObjectList<ITxPlanningInstance> GetAllOtherInstancesInModeling()
  {
    TxObjectList<ITxPlanningInstance> instancesInModeling = new TxObjectList<ITxPlanningInstance>();
    foreach (ITxPlanningInstance instance in (Collection<ITxPlanningInstance>) this.m_selectedInstance.Prototype.Instances)
    {
      if (instance != this.m_selectedInstance && ((ITxPlanningObject) instance).EngineeringRepresentation is ITxComponent engineeringRepresentation && TcCommandsUtility.IsInModeling(engineeringRepresentation))
        ((Collection<ITxPlanningInstance>) instancesInModeling).Add(instance);
    }
    return instancesInModeling;
  }

  public void ReplaceJt()
  {
    ITxPlanningPrototype prototype = this.m_selectedInstance.Prototype;
    Dictionary<ITxPlanningPrototype, string> dictionary = new Dictionary<ITxPlanningPrototype, string>();
    dictionary[prototype] = this.NewJtPath;
    ITxTcAnyGlobalServicesProvider servicesProvider = TxApplication.ActiveDocument.PlatformGlobalServicesProvider as ITxTcAnyGlobalServicesProvider;
    if (!(((ITxPlanningObject) this.m_selectedInstance).EngineeringRepresentation is ITxComponent engineeringRepresentation))
      return;
    if (TcCommandsUtility.IsInModeling(engineeringRepresentation))
    {
      if (!(engineeringRepresentation is ITxStorable itxStorable))
        return;
      TxLocalStorage storageObject = itxStorable.StorageObject as TxLocalStorage;
      TxApplication.ActiveUndoManager.ClearAllTransactions();
      storageObject.InitializeJt(this.NewJtPath);
      TxApplication.ActiveUndoManager.ClearAllTransactions();
    }
    else
    {
      this.SaveViewConfiguration();
      servicesProvider.ReplaceJtOfPrototypes(dictionary);
      this.RestoreViewConfiguration();
    }
  }

  public bool SelectedInstanceIsInModelling
  {
    get
    {
      bool instanceIsInModelling = false;
      if (((ITxPlanningObject) this.m_selectedInstance).EngineeringRepresentation is ITxComponent engineeringRepresentation)
        instanceIsInModelling = TcCommandsUtility.IsInModeling(engineeringRepresentation);
      return instanceIsInModelling;
    }
  }

  private void Initialize()
  {
    TxObjectList<ITxPlanningInstance> instances = this.m_selectedInstance.Prototype.Instances;
    this.Instances = new List<string>();
    foreach (ITxObject itxObject in (Collection<ITxPlanningInstance>) instances)
      this.Instances.Add(itxObject.Name);
    this.Instances.Sort();
    if (((ITxObject) this.m_selectedInstance).GetAttribute("COMPONENT_SET_JT_FILE_NAME") is TxStringAttribute attribute)
      this.CurrentJtName = attribute.Value;
    else
      this.CurrentJtName = Path.GetFileName(this.getCurrentJtFilePath());
  }

  private string getCurrentJtFilePath()
  {
    string currentJtFilePath = "";
    TxFileAttachment[] fileAttachments = ((ITxPlanningObject) this.m_selectedInstance).FileAttachments;
    for (int index = 0; string.IsNullOrEmpty(currentJtFilePath) && index < fileAttachments.Length; ++index)
    {
      string pathInFileSystem = fileAttachments[index].FullPathInFileSystem;
      if (pathInFileSystem != null && pathInFileSystem.EndsWith(".jt", StringComparison.InvariantCultureIgnoreCase))
        currentJtFilePath = pathInFileSystem;
    }
    return currentJtFilePath;
  }

  private void SaveViewConfiguration()
  {
    this.m_savedCamera = TxApplication.ViewersManager.GraphicViewer.CurrentCamera;
    this.m_savedSelection = new List<TxProcessModelId>();
    foreach (ITxObject planningItem in (Collection<ITxObject>) TxApplication.ActiveSelection.GetPlanningItems())
    {
      if (planningItem is ITxPlanningObject itxPlanningObject)
        this.m_savedSelection.Add(itxPlanningObject.ProcessModelId);
    }
  }

  private void RestoreViewConfiguration()
  {
    TxApplication.ViewersManager.GraphicViewer.CurrentCamera = this.m_savedCamera;
    TxObjectList txObjectList = new TxObjectList();
    foreach (TxProcessModelId txProcessModelId in this.m_savedSelection)
    {
      ITxObject byProcessModelId = TxApplication.ActiveDocument.GetObjectByProcessModelId(txProcessModelId);
      if (byProcessModelId != null)
        ((Collection<ITxObject>) txObjectList).Add(byProcessModelId);
    }
    TxApplication.ActiveSelection.SetItems(txObjectList);
  }
}
